/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
<?php
$project_home = $_GET["project_home"];
$req_module_name = $_GET["req_module"];
if(!isset($project_home))
{
	$project_home = "./";
}
$files = array();
$d = dir($project_home);
// Loop through all of the files:
$i = 0;
while (false !== ($file = $d->read()))
{
	// If the file is not this file, and does not start with a '.' or '~'
	// and does not end in LCK, then store it for later display:
	if ( ($file{0} != '.') &&
		 ($file{0} != '~') &&
		 (substr($file, -3) != 'php') )
	{
		if($file == "store")
		{
			continue;
		}
		$files[$i++] = $file;
	}
}

/** handle the sql creatation part */
$link = mysql_connect('localhost', 'root', '');
if(!isset($req_module_name))
{
	if (!$link) 
	{
	   die('Could not connect: ' . mysql_error());
	}
	if (mysql_select_db("semf_test")) //just refresh the database
	{
		mysql_query("drop DATABASE semf_test;");
	}
	
	if (!mysql_query("CREATE DATABASE semf_test"))
	{
	   echo 'Error creating database: ' . mysql_error() . "\n";
	}
}

mysql_select_db("semf_test") || die("Error accessing database: ".mysql_error()."\n");

$sql_types = array(	"Text"=> "VARCHAR(32)",
					"Number"=> "INT",
					"Date"=> "DATE",
					"YES/NO"=>"BOOLEAN");

foreach($files as $file)
{
	$module_name = $file;
	
	if(isset($req_module_name) && $req_module_name != $module_name)
	{
		continue;
	}
	
	
	$module_xml = $project_home.$file."/"."module.xml";
	
	$doc = new DOMDocument();
	$doc->load($module_xml);
	
	if($doc == NULL || $doc->documentElement == NULL)
	{
		exit();
	}
	$table_elements = $doc->documentElement->childNodes;
	
	
	foreach ($table_elements as $table_element)
	{
	
		$sql_text = "";
		$table_name = $table_element->getAttribute("name");
		$sql_text .= "CREATE TABLE ".$module_name."_".$table_name."(\n";
		
		$sql_text .= "ID INT NOT NULL AUTO_INCREMENT,\n";
		
		$sql_text .= "__newly_created BOOLEAN";
		
		$field_elements = $table_element->childNodes;
		foreach($field_elements as $field_element)
		{
			$midway_sql_text = "";
			
			$field_name = $field_element->getAttribute("name");
			$field_type = $field_element->getAttribute("type");
			
			$is_array = $field_element->getAttribute("isarray");
			
			
			$sql_type = $sql_types[$field_type];
			if($sql_type == NULL)
			{
				//this means this is a table
				$sql_type = "INT";
				$field_type = substr($field_type, 6);
				$associated_table_name = str_replace(".", "_", $field_type);
			}
			if($is_array)
			{
				$midway_sql_text .= 
					"CREATE TABLE ".$module_name."_".$table_name."_".$field_name."(\n";
				$midway_sql_text .= "ID INT NOT NULL,\n";
				$midway_sql_text .= $field_name." ".$sql_type.",\n";
				$midway_sql_text .= "PRIMARY KEY(ID, ".$field_name.")";
				$midway_sql_text .= ");\n\n";
				//mysql_query($midway_sql_text) || die('MySQL Error: '. mysql_error());
				execute_query($midway_sql_text);
			}
			else
			{
				$sql_text .= ",\n";
				$sql_text .= $field_name." ";
				$sql_text .= $sql_type;
			}
			
		}
		
		$sql_text .= ",\n";
		$sql_text .= "PRIMARY KEY(ID)";
		
		$sql_text .= ");\n\n";
		//mysql_query($sql_text) || die('MySQL Error: '. mysql_error());
		//$sql_text .= $midway_sql_text;
		execute_query($sql_text);
	}
}

function execute_query($sql)
{
	mysql_query($sql); //no need to report error
	
	echo str_replace("\n", "<br/>\n", $sql)."\n";
}
//echo $sql_text;

// Free resultset
//mysql_free_result($result);

// Closing connection
mysql_close($link);
?>
